<?php
class Order_Model_Order
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Order_Model_DbTable_Order();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet 
	*@return boolean
	*/
	public  function create($row){
 		return $this->_table->insert($row); 
	}

	/**
	*Update One Row  
	*@param rowSet $row 
	*@param int $order_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$row['last_modified_time'] =  date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto("order_id= ?", $id);
		return $this->_table->update($row, $where);
	}
	/**
	*Update One Row  
	*@param rowSet $row 
	*@param int $orders_code 
	*@return boolean
	*/
	public  function updateByOrdersCode($row,$orders_code){
		$where = $this->_table->getAdapter()->quoteInto("orders_code= ?", $orders_code);
		return $this->_table->update($row, $where);
	}
	
	
	public  function updateByOrdersIds($row,$orderIds){
		$where = $this->_table->getAdapter()->quoteInto("order_id in(?)", $orderIds);
		return $this->_table->update($row, $where);
	}
	
	
	public  function getByOrdersIds($orderIds){
		
		$where = $this->_table->getAdapter()->quoteInto("order_id in(?)", $orderIds);
		return $this->_table->fetchAll($where);
		
	}
	
	public  function getByOrderCodesAllIn($orderCodes){
	   
		$where = $this->_table->getAdapter()->quoteInto(" process_status>0 And process_status<5 And orders_code in(?)", $orderCodes);
		return $this->_table->fetchAll($where);
	
	}
	
	
	
	/**
	*Delete One Row
	*@param int $order_id 
	*@return boolean
	*/
	public function delete($id){		
		$where = $this->_table->getAdapter()->quoteInto("order_id= ?", $id);
		return $this->_table->delete($where);
	}
	public function groupByShipMethod($condition=array(),$orderby=array()){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,"shipping_method,count(*) count");
		if(isset($condition["orders_status"]) && $condition["orders_status"]!=''){
			$select->where(" orders_status = ?",$condition["orders_status"]);
		}
		if(isset($condition["process_status"]) && $condition["process_status"]!=''){
			$select->where(" process_status = ?",$condition["process_status"]);
		}
		if(isset($condition["import_status"]) && $condition["import_status"]!=''){
			$select->where(" import_status in (?)",$condition["import_status"]);
		}
		if(isset($condition["orders_print_type"]) && $condition["orders_print_type"]!=''){
			$select->where(" orders_print_type in (?)",$condition["orders_print_type"]);
		}
	
		$select->group("shipping_method");
		$select->order($orderby);
		$sql=$select->__toString();
// 		print_r($sql);exit;
		$result =  $this->_table->getAdapter()->fetchAll($sql);

		return $result;
		
	}
	
    public function getByCondition($condition=array(),$type='*',$orderby=array('order_id'),$pageSize=0,$page=1){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,$type);
		$select->where("1 =?",1);
//		print_r($condition);
		
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
			$select->where(" customer_id = ?",$condition["customer_id"]);
		}
		if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
			$select->where(" warehouse_id = ?",$condition["warehouse_id"]);
		}
		if(isset($condition["consignee_country_id"]) && $condition["consignee_country_id"]!=''){
			$select->where(" consignee_country_id = ?",$condition["consignee_country_id"]);
		}
		
		if(isset($condition["orders_status"]) && $condition["orders_status"]!=''){
			$select->where(" orders_status = ?",$condition["orders_status"]);
		}
		if(isset($condition["underreview_status"]) && $condition["underreview_status"]!=''){
			$select->where(" underreview_status = ?",$condition["underreview_status"]);
		}
		
		if(isset($condition["import_status"])&&$condition["import_status"]!=''){
			$select->where(" import_status = ?",$condition["import_status"]);				
		}
		
		if(isset($condition["process_status"])&&$condition["process_status"]!=''){
			$select->where(" process_status = ?",$condition["process_status"]);				
		}
// 		else{
// 			$select->where(" process_status >0",$condition["process_status"]);				
// 		}
		
		if(isset($condition["stop_status"])&&$condition["stop_status"]!=''){
			$select->where(" stop_status = ?",$condition["stop_status"]);
		}
		
		if(isset($condition["pickup_user_id"])&&$condition["pickup_user_id"]!=''){
			$select->where(" pickup_user_id = ?",$condition["pickup_user_id"]);
		}
		
		if(isset($condition["orders_code"])&&!empty($condition["orders_code"])){			
			$select->where(" orders_code in (?)",$condition["orders_code"]);				
		}
		
		if(isset($condition["payment_status"]) && $condition["payment_status"]!=''){
			$select->where(" payment_status = ?",$condition["payment_status"]);
		}

		if(isset($condition["add_time"]) && $condition["add_time"]!=''){
			$select->where(" add_time = ?",$condition["add_time"]);
		}
		if(isset($condition["shipping_method"]) && $condition["shipping_method"]!=''){
			$select->where(" shipping_method = ?",$condition["shipping_method"]);
		}
		
		if(isset($condition["order_date_from"]) && $condition['order_date_from'] != ""){
			$condition['order_date_from']=date('Y-m-d',strtotime("{$condition['order_date_from']}"))." 00:00:00";
			$select->where("add_time>=?",$condition['order_date_from']);
		}
		if(isset($condition["order_date_to"]) && $condition['order_date_to'] != ""){
			$condition['order_date_to']=date('Y-m-d',strtotime("{$condition['order_date_to']}"))." 23:59:59";
			$select->where("add_time<=?",$condition['order_date_to']);
		}
		$dateColumn = "add_time";
		if(!isset($condition["dateType"]) || $condition['dateType'] == ""){
			$condition["dateType"] = "add_time";
		}
		
		if($condition["dateType"]=="ship_time"){
			$dateColumn="ship_date";
		}elseif ($condition["dateType"]=="print_time"){
			$dateColumn="print_time";
		}
		if(isset($condition["dateFrom"]) && $condition['dateFrom'] != ""){			
			//$condition['dateFrom']=date('Y-m-d',strtotime("{$condition['dateFrom']}"))." 00:00:00";
			$select->where(" $dateColumn>=?",$condition['dateFrom']);
		}
		if(isset($condition["dateTo"]) && $condition['dateTo'] != ""){			
			//$condition['dateTo']=date('Y-m-d',strtotime("{$condition['dateTo']}"))." 23:59:59";
			$select->where(" $dateColumn<=?",$condition['dateTo']);
		}
		
		/**报表时间段 start**/
		if(isset($condition["date_from"]) && $condition["date_from"]!=''){
			$select->where(" add_time >= ?",$condition["date_from"]);
		}
		if(isset($condition["date_to"]) && $condition["date_to"]!=''){
			$select->where(" add_time <?",$condition["date_to"]);
		}
		/****报表时间段 end***/
		
		/**报表时间段 start**/
		if(isset($condition["ship_date_from"]) && $condition["ship_date_from"]!=''){
			$select->where(" ship_date >= ?",$condition["ship_date_from"]);
		}
		if(isset($condition["ship_date_to"]) && $condition["ship_date_to"]!=''){
			$select->where(" ship_date <?",$condition["ship_date_to"]);
		}
		/****报表时间段 end***/
		
		if(isset($condition["orderType"]) &&$condition['orderType']==0){//一票一件
			$select->where(" orders_print_type=?",0);
		}else if(isset($condition["orderType"]) &&$condition['orderType']>0){//一票多件
			$select->where(" orders_print_type>?",0);
		}
		
		
		if(isset($condition["ws_code_null"])){
			$select->where(" ws_code =?",$condition["ws_code_null"]);
		}
// 			echo $select;exit;
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchOne($select);
		}else{
			$select->order($orderby);
			if($pageSize>0 and $page>0)
			{
				$start =($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			$sql=$select->__toString();
			$result =  $this->_table->getAdapter()->fetchAll($sql);

// 			print_r($sql);exit;
			return $result;
		}
	}
	
	public function getStopByCondition($condition=array(),$type='*',$orderby=array('order_id'),$pageSize=0,$page=1){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,$type);
	
	
		if(isset($condition["underreview_status"])&&$condition["underreview_status"]!=''){
			$select->where(" underreview_status = ?",$condition["underreview_status"]);
		}else{
			$select->where(" underreview_status >0 ",$condition["underreview_status"]);			
		}
	
	
		if(isset($condition["orders_code"])&&!empty($condition["orders_code"])){
			$select->where(" orders_code = ? ",$condition["orders_code"]);
		}
		
		if(isset($condition["shipping_method"])&&!empty($condition["shipping_method"])){
			$select->where(" shipping_method = ? ",$condition["shipping_method"]);
		}
		
		if(isset($condition["orders_status"])&&$condition["orders_status"]!=''){
			$select->where(" orders_status = ?",$condition["orders_status"]);
		}else{
			$select->where(" orders_status >0 ");
		}
		
		if(isset($condition["stop_date_from"]) && $condition['stop_date_from'] != "" ){
			$condition['stop_date_from']=date('Y-m-d',strtotime("{$condition['stop_date_from']}"))." 00:00:00";
			$select->where("order_stop_date>=?",$condition['stop_date_from']);
		}
		if(isset($condition["stop_date_to"]) && $condition['stop_date_to'] != ""){
			
			$condition['stop_date_to']=date('Y-m-d',strtotime("{$condition['stop_date_to']}"))." 23:59:59";
			$select->where("order_stop_date<=?",$condition['stop_date_to']);
		}
		
		
		if(isset($condition["process_status"])&&$condition["process_status"]!=''){
			$select->where(" process_status = ?",$condition["process_status"]);
		}else{
			$select->where(" process_status >0 ");
		}
		
		
	
	
		if('count(*)'==$type){	
			//echo $select;exit;
			return $this->_table->getAdapter()->fetchOne($select);
		}else{
			$select->order($orderby);
			if($pageSize>0 and $page>0)
			{
				$start =($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			$sql=$select->__toString();
			$result =  $this->_table->getAdapter()->fetchAll($sql);
	
			//print_r($sql);exit;
			return $result;
		}
	}
	
	/**
	*Get Rowset  
	*@param string $order_id 
	*@return Rowset
	*/
	public function getById($id){
		$where = $this->_table->getAdapter()->quoteInto('order_id= ?', $id);
		$result =  $this->_table->fetchRow($where);
		if($result instanceof Zend_Db_Table_Row){
			return $result->toArray();
		}
		return $result;
	}
	
	
	/**
	*Get Rowset  
	*@param string $order_code
	*@return Rowset
	*/
	public function getByOrdersCode($orders_code){
		$where = $this->_table->getAdapter()->quoteInto('orders_code= ?', $orders_code);
		return $this->_table->fetchRow($where);
	}
	
	public function getByfield($field=1,$value=0){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where);
	}
// 	方法废弃
	public function getByCategoryAndOrderType($orders_code='',$category=0,$orderType=0,$customerID='',$type='*',$page=1,$pageSize=0,$orderBy=''){
		$adapter = $this->_table->getAdapter();
		
		$sql = "select $type from orders where 1=1 ";
		if(isset($orders_code)&&!empty($orders_code)){
			$sql .= $adapter->quoteInto(" and orders_code = ?",$orders_code);			
		}
		switch($orderType){
			case 0://一票一件
			case 1://单个sku	
				if(!empty($category)){
					$sql .= $adapter->quoteInto(" and  orders_code in(select orders_code from order_product where order_product.op_category = ? ) ",$category);					
				}			
				break;
		}
		
		if(!empty($customerID)){
			$sql .= $adapter->quoteInto(" and customer_id = ?",$customerID);			
		}
		if($orderType==0){
			$sql .= $adapter->quoteInto(" and orders_print_type in (?)",array(0,1));			
		}else{
			$sql .= $adapter->quoteInto(" and orders_print_type = ?",$orderType);			
		}
		$sql .= $adapter->quoteInto(" and orders_status = ?",3);
		$sql .= $adapter->quoteInto(" and process_status = ?",1);
//		echo $sql;exit;
		if('count(*)'==$type){
//			echo $sql;exit;
//			return 10;
			return $adapter->fetchOne($sql);
		}else{			
			$sql .= "  order by $orderBy";
			
			$sql .= " limit ".($pageSize*($page-1)).", $pageSize";
//			echo $sql;exit;
			return $adapter->fetchAll($sql);
		}
	}
	
	public function getByCategoryAndOrderTypeNew($condition,$type='*',$page=1,$pageSize=0,$orderBy=''){
		$adapter = $this->_table->getAdapter();
		
		$sql = "select $type,case when  customer_order_code like '%AM%' then 1 else 0 end as has_am from orders where 1=1 ";
		if(isset($condition['orders_code'])&&!empty($condition['orders_code'])){
			$sql .= $adapter->quoteInto(" and orders_code = ?",$condition['orders_code']);			
		}
		
		if(isset($condition['orders_code_list'])&&!empty($condition['orders_code_list'])){
			$sql .= $adapter->quoteInto(" and orders_code in(?)",$condition['orders_code_list']);
		}
		if(isset($condition['shipping_method'])&&!empty($condition['shipping_method'])){
			$sql .= $adapter->quoteInto(" and shipping_method = ?",$condition['shipping_method']);	
					
		}		
		
		if(isset($condition['import_status'])&&!empty($condition['import_status'])){
			$sql .= $adapter->quoteInto(" and import_status in(?)",$condition['import_status']);
		}
		
		$dateColumn = "add_time";
		if(!isset($condition["dateType"]) || $condition['dateType'] == ""){
			$condition["dateType"] = "add_time";
		}
		
		if($condition["dateType"]=="ship_time"){
			$dateColumn="ship_date";
		}elseif ($condition["dateType"]=="print_time"){
			$dateColumn="print_time";
		}
		if(isset($condition["dateFrom"]) && $condition['dateFrom'] != ""){
			$condition['dateFrom']=date('Y-m-d H:i:s',strtotime("{$condition['dateFrom']}"));
			$sql .= $adapter->quoteInto(" and $dateColumn>=?",$condition['dateFrom']);
		}
		if(isset($condition["dateTo"]) && $condition['dateTo'] != ""){
			$condition['dateTo']=date('Y-m-d H:i:s',strtotime("{$condition['dateTo']}"));
			$sql .= $adapter->quoteInto(" and $dateColumn<=?",$condition['dateTo']);
		}
		
		if(!empty($condition['category'])&&$condition['category']!=""){
			$sql .= $adapter->quoteInto(" and  orders_code in(select orders_code from order_product where order_product.op_category = ? ) ",$condition['category']);
		}
		
		if(!empty($condition['customerID'])){
			$sql .= $adapter->quoteInto(" and customer_id = ?",$condition['customerID']);			
		}
//		if($condition['orderType']>-1){
//			$sql .= $adapter->quoteInto(" and orders_print_type = ?",$condition['orderType']);			
//		}
		if(isset($condition["orderType"]) &&$condition['orderType']!=""){//一票一件
			if($condition['orderType']=="0"){
				$sql .= $adapter->quoteInto(" and orders_print_type <=?",1);
			}elseif($condition['orderType']>0){
				$sql .= $adapter->quoteInto(" and orders_print_type > ?",1);
			}		
		}
		
		if(isset($condition["orderPrintType"]) && $condition['orderPrintType'] !=""){
			if($condition["orderPrintType"]=='0'){
				$sql .= $adapter->quoteInto(" and orders_print_type in (?)",array(0));
			}else{
				$sql .= $adapter->quoteInto(" and orders_print_type in (?)",array(1,2));
			}
				
		}
		
		
		
		$sql .= $adapter->quoteInto(" and orders_status = ?",3);
		$sql .= $adapter->quoteInto(" and process_status = ?",1);
// 		echo $sql;exit;
		if('count(*)'==$type){
// 			echo $sql;exit;
//			return 10;
			return $adapter->fetchOne($sql);
		}else{			
			$sql .= "  order by has_am desc ,$orderBy";
			
			$sql .= " limit ".($pageSize*($page-1)).", $pageSize";
			//print_r($adapter->fetchAll($sql));exit;
 			//echo $sql;exit;
			return $adapter->fetchAll($sql);
		}
	}
	
	public function getByValue($value,$field='order_id',$type='*'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
	
	/**
	 * 获取产品两种状态的所有订单信息
	 *
	 */
	public function getByTwoStatus($orderCode){        
        $sql = "SELECT `orders_code`,`process_status` FROM `orders` WHERE orders_code='".$orderCode."'"; 
		$sql .= " AND ( process_status=3 OR  process_status=4)";
//        print_r($sql);exit();
		return $this->_table->getAdapter()->fetchAll($sql); 
	}
	
	
}